package com.lora.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Slf4j
@Component
public class Everyday {
    @Autowired
    public JdbcTemplate jdbcTemplate;

    /**
     * 每日任务
     */
    public void query1() {
       // Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap("SELECT count(1) as count from lora_alarm_list where DATE_FORMAT(create_time,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d')");
        //Long count =(Long)stringObjectMap.get("count");
        //if(count==0){
            jdbcTemplate.update("INSERT INTO `register` ( `name`, `explain`, `remarks`, `creattime`) VALUES ( 'admin', '市电输入正常、UPS输入输出正常、空调运行正常、温湿度正常、消防系统正常', '市电输入正常、UPS输入输出正常、空调运行正常、温湿度正常、消防系统正常', now())");
        //}
    }

    /**
     * 半年任务
     */
    public void query2() {
        Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap("\n" +
                "SELECT count(1) as count from (\n" +
                "SELECT * from lora_alarm_list where field_title='UPS电池放电' ORDER BY create_time desc LIMIT 1 \n" +
                ") a " +
                "where DATE_FORMAT( DATE_ADD(create_time, INTERVAL 6 MONTH),'%Y-%m-%d')<=DATE_FORMAT(now(),'%Y-%m-%d')");
        Map<String, Object> stringObjectMap1 = jdbcTemplate.queryForMap("\n" +
                "SELECT count(1) as count from (\n" +
                "SELECT * from lora_alarm_list where field_title='机房防雷检测' ORDER BY create_time desc LIMIT 1 \n" +
                ") a " +
                "where DATE_FORMAT( DATE_ADD(create_time, INTERVAL 12 MONTH),'%Y-%m-%d')<=DATE_FORMAT(now(),'%Y-%m-%d')");
        Map<String, Object> map = jdbcTemplate.queryForMap("SELECT count(1) as count from lora_alarm_list where field_title='UPS电池放电' ORDER BY create_time desc ");
        Map<String, Object> map1 = jdbcTemplate.queryForMap("SELECT count(1) as count from lora_alarm_list where field_title='机房防雷检测' ORDER BY create_time desc");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long c =(Long)map.get("count");
        Long c1 =(Long)map1.get("count");
        String time=dateFormat.format(new Date());
        Long count =(Long)stringObjectMap.get("count");
        Long count1 =(Long)stringObjectMap1.get("count");
        if(count>0||c==0){
            jdbcTemplate.update("INSERT INTO `lora_alarm_list` (`create_time`, `update_time`, `alarm_info`, `ep_id`, `field_name`, `field_title`, `handle_status`, `sms_state`)\n" +
                    "  VALUES ( '"+time+"', '"+time+"', 'UPS电池放电', '', 'UPS电池放电', 'UPS电池放电', '0', '0');");
        }
        if(count1>0||c1==0){
            jdbcTemplate.update("INSERT INTO `lora_alarm_list` (`create_time`, `update_time`, `alarm_info`, `ep_id`, `field_name`, `field_title`, `handle_status`, `sms_state`)\n" +
                    "  VALUES ('"+time+"', '"+time+"', '机房防雷检测', '', '机房防雷检测', '机房防雷检测', '0', '0');");
        }
    }
}
